#ifndef NETWORK_H_
#define NETWORK_H_

#include "stdlib.h"

#include <SDL/SDL_net.h>

#include "log_controller.h"
#include "global.h"

#define NET_TCP_PAYLOAD 256
#define NET_MAX_CONNECTIONS 10

typedef enum {
	NET_DISCONNECTED,
	NET_CONNECTED,
	NET_LISTENING
} net_status;

class network {
public:
	network();
	virtual ~network();

	int check();

	/* static methods */
	static int resolve(const char *host, Uint16 port, IPaddress *ip);
	static int receive(void *buf, size_t size, TCPsocket socket, size_t *bytes);
	static int send(void *buf, size_t size, TCPsocket socket, size_t *bytes);
	static int close(TCPsocket socket);

	/* client methods */
	int connect(const char *host, Uint16 port);
	int send(void *buf, size_t size, size_t *bytes);
	int receive(void *buf, size_t max_size, size_t *bytes);
	int close();

	/* Socket set based methods */
	int receive(void *buf, size_t max_size, TCPsocket *socket, size_t *bytes);

protected:
	net_status status;
	TCPsocket conn_list[NET_MAX_CONNECTIONS];
	int conn_count;
	SDLNet_SocketSet sockset;
};

#endif /* NETWORK_H_ */
